<template>
  <el-upload
    class="avatar-uploader"
    action="http://www.cetastar.com/api/upload"
    :show-file-list="false"
    :headers="{ Authorization: identity.authorization }"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img
      v-if="publicStore.AdminUser.profile_picture"
      :src="`http://www.cetastar.com/img/${publicStore.AdminUser.profile_picture}`"
      class="avatar"
    />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
  <span
    >昵称：{{ publicStore.AdminUser.name
    }}<el-icon style="color: #409eff; margin-left: 12px" @click="openEdit"
      ><Edit /></el-icon
  ></span>
  <el-dialog v-model="editVisible" title="修改昵称" width="240">
    <el-input v-model="AdminUser.name" placeholder="请输入昵称" /><el-button
      type="primary"
      @click="editAdmin(AdminUser)"
      style="margin-top: 12px"
      >确定</el-button
    >
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";
import { adminUser, identity } from "../../stores/identity";
import { usePublicStore } from "../../stores/public";

import { requestAxios } from "../../axios";
import { ref } from "vue";
interface AdminUserType {
  adminId: string;
  name: string;
  profile_picture: string;
}
const publicStore = usePublicStore();
const editVisible = ref(false);
const AdminUser = ref(JSON.parse(JSON.stringify(publicStore.AdminUser)));
const handleAvatarSuccess: UploadProps["onSuccess"] = (response) => {
  if (response[0]) {
    // publicStore.AdminUser.profile_picture=response[0].filename
    editAdmin({
      adminId: AdminUser.value.adminId,
      name: AdminUser.value.name,
      profile_picture: response[0].filename,
    });
    return;
  }
};
const openEdit = () => {
  editVisible.value = true;
  AdminUser.value = JSON.parse(JSON.stringify(publicStore.AdminUser));
};
const editAdmin = (admin: AdminUserType) => {
  requestAxios
    .post("/adminuser/edit", admin)
    .then((res) => {
      if (res.code === 0) {
        ElMessage.success(res.msg);
        adminUser.profile_picture = res.data.profile_picture;
        adminUser.adminId = res.data.adminId;
        adminUser.name = res.data.name;
        publicStore.changeAdminUser(res.data);
        editVisible.value = false;
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
